<?php
namespace App\Http\Controllers\Home;
use App\Services\BannerService;
use App\Services\BusinessService;
use App\Services\CategoryService;
use App\Services\CourseService;
use App\Services\LawyerService;
use App\Services\NewsService;
use App\Services\SettingsService;
use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;


class IndexController extends BaseController {

    public function __construct() {
        $info = (new SettingsService())->info(3);
        $this->view_data['setting'] = json_decode($info['content'], true);
    }

    /**
     * 首页
     */
    public function index(Request $request) {
        $this->view_data['banner_list'] = (new BannerService())->getList();
        $about = (new SettingsService())->info(1);
        $this->view_data['about'] = $about;

        // 发展历程
        $course_list = (new CourseService())->list(['home'=>1, 'limit' => 999]);
        $this->view_data['course_list'] = $course_list;

        // 专业领域
        $business_list = (new CategoryService())->list(['home'=>1, 'limit' => 999]);
        $this->view_data['business'] = $business_list;

        // 律师
        $lawyer_list = (new LawyerService())->list(["keyword"=>["合伙人", "高级合伙人"], 'home'=>1, 'limit' => 999]);
        //dd($lawyer_list);
        $lawyer = (new LawyerService())->list(["keyword"=>["实习律师", "专职律师", "高级律师", "全国优秀律师", "专家律师"], 'home'=>1, 'limit' => 999]);
        $this->view_data['lawyer_list'] = $lawyer_list;
        $this->view_data['lawyer'] = $lawyer;

        // 新闻
        $news_list = (new NewsService())->getList(["news_type" => 2, "img"=>true, 'limit' => 4]);
        $this->view_data['news_list'] = $news_list['list'];
        return view('home.index', $this->view_data);
    }

    /**
     * 社会责任首页
     */
    public function sh_index(Request $request) {
        $this->view_data['banner_list'] = (new BannerService())->getList();
        shuffle($this->view_data['banner_list']);
        $this->view_data['list'] = (new NewsService())->getList(["news_type"=>1, "page"=>1, "limit"=>20]);

        return view('home.sh_index', $this->view_data);
    }

    /**
     * 社会责任详情（新闻详情）
     */
    public function sh_detail(Request $request) {
        $news_id = $request->get("id", 0);
        if (empty($news_id) || !is_numeric($news_id)){
            return redirect('/sh/index');
        }

        $news = (new NewsService())->info($news_id);
        if (empty($news)) {
            return redirect('/sh/index');
        }
        $this->view_data['banner_list'] = (new BannerService())->getList();
        shuffle($this->view_data['banner_list']);
        $this->view_data['info'] = $news;
        $this->view_data['up'] = (new NewsService())->up_down($news_id, 'up');
        $this->view_data['down'] = (new NewsService())->up_down($news_id, 'down');

        return view("home.sh_detail", $this->view_data);
    }

    /**
     * 新闻首页
     */
    public function news_index(Request $request) {
        $params = $request->all();
        $params['news_type'] = 2;
        $params['home'] = 1;
        $list = (new NewsService())->getList($params);
        #dd($list);
        $this->view_data['banner_list'] = (new BannerService())->getList();
        shuffle($this->view_data['banner_list']);
        $this->view_data['list'] = $list;
        return view("home.news_index", $this->view_data);
    }

    /**
     * 新闻详情
     */
    public function news_detail(Request $request) {
        $news_id = $request->get("id", 0);
        if (empty($news_id) || !is_numeric($news_id)){
            return redirect('/news/index');
        }

        $news = (new NewsService())->info($news_id);
        if (empty($news)) {
            return redirect('/news/index');
        }
        $this->view_data['banner_list'] = (new BannerService())->getList();
        shuffle($this->view_data['banner_list']);
        $this->view_data['info'] = $news;
        $this->view_data['up'] = (new NewsService())->up_down($news_id, 'up');
        $this->view_data['down'] = (new NewsService())->up_down($news_id, 'down');

        return view("home.sh_detail", $this->view_data);
    }

    /**
     * 律师列表
     */
    public function lawyer(Request $request) {
        $params = $request->all();
        $params['news_type'] = 2;
        $params['home'] = 1;
        $list = (new LawyerService())->list($params);
        #dd($list);
        $this->view_data['banner_list'] = (new BannerService())->getList();
        shuffle($this->view_data['banner_list']);
        $this->view_data['list'] = $list;
        return view("home.lawyer_index", $this->view_data);
    }

    /**
     * 律师详情
     */
    public function lawyer_detail(Request $request) {
        $info_id = $request->get("id", 0);
        if (empty($info_id) || !is_numeric($info_id)){
            return redirect('/lawyer/index');
        }

        $news = (new LawyerService())->info($info_id);
        if (empty($news)) {
            return redirect('/lawyer/index');
        }
        $this->view_data['banner_list'] = (new BannerService())->getList();
        shuffle($this->view_data['banner_list']);
        $this->view_data['info'] = $news;
        //$this->view_data['up'] = (new NewsService())->up_down($info_id, 'up');
        //$this->view_data['down'] = (new NewsService())->up_down($info_id, 'down');

        return view("home.lawyer_detail", $this->view_data);
    }

    /**
     * 专业领域
     */
    public function business(Request $request) {
        $params = $request->all();
        $params['home'] = 1;
        $list = (new CategoryService())->list($params);
        #dd($list);
        $this->view_data['banner_list'] = (new BannerService())->getList();
        shuffle($this->view_data['banner_list']);
        $this->view_data['list'] = $list;
        return view("home.business_index", $this->view_data);
    }

    /**
     * 某个专业领域的列表
     */
    public function business_list(Request $request) {
        $params = $request->all();
        $params['home'] = 1;
        $list = (new BusinessService())->list($params);
        $category = (new CategoryService())->info($params['class_id']);
        //dd($list);
        $this->view_data['banner_list'] = (new BannerService())->getList();
        shuffle($this->view_data['banner_list']);
        $this->view_data['list'] = $list;
        $this->view_data['category'] = $category;
        return view("home.business_list", $this->view_data);
    }

    /**
     * 专业领域详情
     */
    public function business_detail(Request $request) {
        $news_id = $request->get("id", 0);
        if (empty($news_id) || !is_numeric($news_id)){
            return redirect('/business/index');
        }

        $news = (new BusinessService())->info($news_id);
        if (empty($news)) {
            return redirect('/business/index');
        }
        $this->view_data['banner_list'] = (new BannerService())->getList();
        shuffle($this->view_data['banner_list']);
        $this->view_data['info'] = $news;
        $this->view_data['up'] = (new BusinessService())->up_down($news_id, 'up');
        $this->view_data['down'] = (new BusinessService())->up_down($news_id, 'down');

        return view("home.business_detail", $this->view_data);
    }

    /**
     * 联系我们
     */
    public function contact_index(Request $request) {
        $info = (new SettingsService())->info(2);
        $this->view_data['banner_list'] = (new BannerService())->getList();
        shuffle($this->view_data['banner_list']);
        $this->view_data['info'] = $info;
        return view("home.contact_index", $this->view_data);
    }

    /**
     * 关于我们
     */
    public function about_index(Request $request) {
        $info = (new SettingsService())->info(1);
        $this->view_data['banner_list'] = (new BannerService())->getList();
        shuffle($this->view_data['banner_list']);
        $this->view_data['info'] = $info;
        return view("home.about_index", $this->view_data);
    }

    /**
     * 发展历程详情
     */
    public function course_detail(Request $request) {
        $news_id = $request->get("id", 0);
        if (empty($news_id) || !is_numeric($news_id)){
            return redirect('/business/index');
        }
        $info = (new CourseService())->info($news_id);
        #dd($list);
        $this->view_data['banner_list'] = (new BannerService())->getList();
        shuffle($this->view_data['banner_list']);
        $this->view_data['info'] = $info;
        $this->view_data['up'] = (new BusinessService())->up_down($news_id, 'up');
        $this->view_data['down'] = (new BusinessService())->up_down($news_id, 'down');
        return view("home.course_detail", $this->view_data);
    }
}